<template>
    <div style="background-color: #ffffff; padding: 50px 120px 30px 30px;">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="旧密码" prop="passold">
          <el-input type="password" v-model="ruleForm.passold" placeholder="请输入" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="pass">
          <el-input type="password" v-model="ruleForm.pass" placeholder="请输入" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" placeholder="请输入" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
</template>

<script>
    export default {
      name: "PasswordProfile",
      data() {
        //判断旧密码是否正确
        const validatePassold = (rule,value,callback) => {
          if (value === '') {
            callback(new Error('请输入旧密码'));
          } else {
            this.$http({
              url: '/user/checkOldPwd',
              method: 'post',
              data:  this.ruleForm.passold
            }).then(res => {
              const ispwd = res.data
              if (!ispwd){
                callback(new Error('旧密码输入错误'))
              } else {
                callback()
              }
            }).catch(err => {
              console.log(err);
            })
          }
        };
        //输入新密码
        const validatePass = (rule, value, callback) => {
          if (value === '') {
            callback(new Error('请输入新密码'));
          } else {
            if (value === this.ruleForm.passold){
              callback(new Error('与旧密码一致，请重新输入'));
            } else {
              const reg = /^[\w]{6,15}$/i
              const reg1 = /[a-zA-Z]/
              const reg2 = /[0-9]/
              if ( !reg.test(value)) {
                callback(new Error('密码格式不正确'));
              }else {
                if (this.ruleForm.checkPass !== '') {
                  this.$refs.ruleForm.validateField('checkPass');
                }
                callback();
              }
            }
          }
        };
        //再次输入新密码
        const validatePass2 = (rule, value, callback) => {
          if (value === '') {
            callback(new Error('请再次输入密码'));
          } else if (value !== this.ruleForm.pass) {
            callback(new Error('两次输入密码不一致!'));
          } else {
            callback();
          }
        };
        return {
          ruleForm: {
            pass: '',
            checkPass: '',
            passold: ''
          },
          rules: {
            pass: [
              { validator: validatePass, trigger: 'blur' }
            ],
            checkPass: [
              { validator: validatePass2, trigger: 'blur' }
            ],
            passold: [
              { validator: validatePassold, trigger: 'blur' }
            ]
          }
        };
      },
      methods: {
        //修改成功跳到主页
        submitForm(formName) {
          this.$refs[formName].validate((valid) => {
            if (valid) {
              this.$http({
                method: 'post',
                url: '/user/updatePwd',
                data: this.ruleForm.checkPass
              }).then(res => {
                this.ruleForm.pass = ''
                this.ruleForm.checkPass = ''
                this.ruleForm.passold = ''
                this.$message({
                  message: '修改成功',
                  type: 'success'
                });
                this.$router.replace('/home')
              }).catch(err => {
                console.log(err);
              })
            } else {
              console.log('error submit!!');
              return false;
            }
          });
        },
        //重置
        resetForm(formName) {
          this.$refs[formName].resetFields();
        }
      }
    }
</script>

<style scoped>

</style>
